package com.bootdo.system.dao;

import com.bootdo.system.domain.BabyDO;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface BabyMapper {

	@Select("select * from tb_baby where id = #{id}")
	BabyDO get(Long id);
	
	@Select("<script>" +
	"select * from tb_baby " + 
			"<where>" + 
			  "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"babyName != null and babyName != ''\">"+ "and babyName = #{babyName} " + "</if>" + 
	  		  "<if test=\"babySex != null and babySex != ''\">"+ "and babySex = #{babySex} " + "</if>" + 
	  		  "<if test=\"birthDay != null and birthDay != ''\">"+ "and birthDay = #{birthDay} " + "</if>" + 
	  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
		  			"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<BabyDO> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select count(*) from tb_baby " + 
			"<where>" + 
		  		  "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
		  		  "<if test=\"babyName != null and babyName != ''\">"+ "and babyName = #{babyName} " + "</if>" + 
		  		  "<if test=\"babySex != null and babySex != ''\">"+ "and babySex = #{babySex} " + "</if>" + 
		  		  "<if test=\"birthDay != null and birthDay != ''\">"+ "and birthDay = #{birthDay} " + "</if>" + 
		  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
		  			"</where>"+ 
			"</script>")
	int count(Map<String,Object> map);
	
	@Insert("insert into tb_baby (`babyName`, `babySex`, `birthDay`, `babyImg`, `ownerId`, `createUserId`, `createTime`)"
	+ "values (#{babyName}, #{babySex}, #{birthDay}, #{babyImg}, #{ownerId}, #{createUserId}, #{createTime})")
	@Options(useGeneratedKeys=true)
	int save(BabyDO bComments);
	
	@Update("<script>"+ 
			"update tb_baby " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"babyName != null\">`babyName` = #{babyName}, </if>" + 
                    "<if test=\"babySex != null\">`babySex` = #{babySex}, </if>" + 
                    "<if test=\"birthDay != null\">`birthDay` = #{birthDay}, </if>" + 
                    "<if test=\"babyImg != null\">`babyImg` = #{babyImg}, </if>" + 
                    "<if test=\"ownerId != null\">`ownerId` = #{ownerId}, </if>" +    
          					"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(BabyDO bComments);
	
	@Delete("delete from tb_baby where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_baby where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
}
